$(function () {
    obj.init();
});

var obj = new function () {
    return {
        dataGrid: null,
        noteStage:null,
        noteProv:null,
        noteName:null,
        noteNum:null,
        noteCity:null,
        height:0,
        tableHeight:0,
        bodyHeight:0,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'res/getAreaBookVerData');
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_del','bt_list_edit']);
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.funObj().setViewHeight();
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().searchList();
                })
            );
            // //双击编辑
            // $this.dataGrid.onDblClickRow = function(rowIndex, rowData){
            //     var groupId = rowData['groupId'];
            //     if (groupId === '' || groupId === null){
            //         window.top.xhTop.showError('数据id为空');
            //         return false;
            //     }
            //     $this.funObj().editAreaGroup(groupId);
            // };
            $this.dataGrid.onLoadSuccess =function () {
                $this.funObj().reHeight();
            };
            //添加
            $('#bt_list_add').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addAreaGroup();
            });
            //删除
            $('#bt_list_del').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().del();
            });
            //编辑
            $('#bt_list_edit').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().edit();
            });
            //剩余地区
            $('#list_outArea').click(function () {
                $this.funObj().getOutArea();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                searchList: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        type: xhExtra.getValue($('#list_area_type')),
                        stage: xhExtra.getValue($('#list_stage'))
                    })
                },
                getOutArea: function () {
                    window.top.xhTop.showModal({
                        title: '待设置地区',
                        content: xhTool.getFrameString('res/getOutArea', null, 250),
                        size: 'sm',
                        width:'600'
                    });
                },
                addAreaGroup: function () {
                    window.top.xhTop.createNewTab('创建区域组', 'res/addAreaGroup');
                },
                editAreaGroup: function (groupId) {
                    window.top.xhTop.createNewTab('修改区域组', 'res/modifyAreaGroup?groupId='+groupId);
                },
                del: function () {
                    var id = $this.dataGrid.getSelectValue('groupId');
                    var area = $this.dataGrid.getSelectValue('area');
                    if (id < 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    if (area !== null && area !== '' && area !== undefined){
                        window.top.xhTop.showError('请先移除所有已设置区域再删除区域组！');
                        return false;
                    }
                    xhExtra.askChangeStatus('res/delAreaBookVer', {id: id}, $this.dataGrid, '该操作无法恢复，请谨慎操作!'+'</br>'+'确认删除？');
                },
                edit: function () {
                    var groupId = $this.dataGrid.getSelectValue('groupId');
                    if (groupId === '' || groupId === null){
                        window.top.xhTop.showError('数据id为空');
                        return false;
                    }
                    $this.funObj().editAreaGroup(groupId);
                },
                setViewHeight: function () {
                    $this.height = $('.datagrid-view').height();
                    $this.tableHeight = $('.datagrid-btable').height();
                    $this.bodyHeight = $('.datagrid-body').height();
                },
                reloadList: function () {
                    $this.dataGrid.reload();
                },
                reHeight: function () {
                    $('.datagrid-header').height('auto');
                    $('.datagrid-view').height($this.height);
                    $('.datagrid-body').height($this.bodyHeight);
                }
            }
        }
    }
};